package com.example.chcrm.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.chcrm.entity.common.ResponseMsg;
import com.example.chcrm.entity.common.ServletUtils;
import com.example.chcrm.entity.employee.EmployeeVo;
import com.example.chcrm.entity.employee.RoleModels;
import com.example.chcrm.entity.vehicle.ShipDeployInfo;
import com.example.chcrm.entity.vehicle.VehicleDeployFleetCv;
import com.example.chcrm.service.ShipDeployInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author 邹
 * @Date 2025/4/27 5:06
 * @Version 1.0
 */
@RestController
@RequestMapping("/shipDeploy")
@CrossOrigin // 允许跨域访问
public class ShipDeployInfoController {
    @Autowired
    private ShipDeployInfoService shipDeployService;


    @PostMapping("/pageList")
    public ResponseMsg<IPage<ShipDeployInfo>> pageList(@RequestBody ShipDeployInfo roleModels) {
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
        return shipDeployService.pageList(roleModels);
    }

    @PostMapping("/add")
    public ResponseMsg<Object> add(@RequestBody ShipDeployInfo sd) {
        ResponseMsg<Object> msg = new ResponseMsg<>();
        if (sd.getStartPortName() == null || sd.getDeliveryPortName() ==null) {
            msg.fail("请至少选择一个装船和卸船的港口");
            return msg;
        }
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
        return  shipDeployService.add(sd, empVo);
    }

    @PostMapping("/loadCars")
    public ResponseMsg<Object> startLoading(@RequestBody ShipDeployInfo deployDriver){
        ResponseMsg<Object> msg = new ResponseMsg<>();
        if(deployDriver == null){
            msg.fail("操作失败，数据不完整");
            return msg;
        }
        if(deployDriver.getId() == null){
            msg.fail("操作失败，水路调配id不能为空");
            return msg;
        }
        if(deployDriver.getStartTime() == null){
            msg.fail("操作失败，装船时间不能为空");
            return msg;
        }
        if(deployDriver.getCarList() == null || deployDriver.getCarList().size() <= 0){
            msg.fail("操作失败，未有委托车装车");
            return msg;
        }
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;

        return shipDeployService.startLoading(deployDriver,empVo);
    }

    @GetMapping("/detail/{id}")
    public ResponseMsg<Object> detail(@PathVariable String id) {
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
        return shipDeployService.detail(id);
    }

    @PostMapping("/unloadCars")
    public ResponseMsg<Object> fulfilTransport(@RequestBody ShipDeployInfo deployDriver){
        ResponseMsg<Object> msg = new ResponseMsg<>();
        if(deployDriver == null){
            msg.fail("操作失败，数据异常");
            return msg;
        }
        if(!StringUtils.isNotBlank(deployDriver.getId())){
            msg.fail("操作失败，操作id不能为空");
            return msg;
        }
        if(deployDriver.getEndTime() == null){
            msg.fail("操作失败，完成时间不能为空");
            return msg;
        }
        HttpServletRequest request = ServletUtils.getRequest();
        EmployeeVo empVo = request != null ? (EmployeeVo) request.getAttribute("empVo") : null;
        return shipDeployService.fulfilTransport(deployDriver,empVo);
    }
}
